package cn.wolfcode.wms.interceptor;

import cn.wolfcode.wms.util.UserContext;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor extends HandlerInterceptorAdapter {

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 从session中获取当前登录的用户
        Object userInSession = UserContext.getcurrentUser();
        // 如果用户不存在，重定向到登录页面
        if (userInSession == null) {
            response.sendRedirect("/login.html");
            return false;
        }
        return true;
    }
}
